package com.itinsys.enma.web;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itinsys.enma.domain.Period;
import com.itinsys.enma.domain.response.PeriodResponse;
import com.itinsys.enma.exception.period.PeriodExistsException;
import com.itinsys.enma.manager.PeriodManager;
import com.itinsys.enma.manager.ResponseManager;

@Controller
@RequestMapping({ "/period" })
public class PeriodFormController extends FormController {
	protected final Log logger = LogFactory.getLog("controller");
	
	@Autowired
	private PeriodManager periodManager;

	@Autowired
	private ResponseManager responseManager;
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ResponseBody
	public PeriodResponse getAllPerdiod() {
		return periodManager.getNotSpecialPeriods();
		
	}
	
	@RequestMapping(value = "/lockControl", method = RequestMethod.GET)
	@ResponseBody
	public PeriodResponse lockControl() {
		return periodManager.lockControl();
		
	}

	@RequestMapping(value = { "/add" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public PeriodResponse addPeriod(@RequestBody Map<String,String> periodDTO,HttpServletResponse response) {
		PeriodResponse resp=new PeriodResponse();
		responseManager.prepareResponse(resp,PeriodResponse.OK);
		Period oPeriod = periodManager.dtoToPeriod(periodDTO);
		periodManager.addPeriod(oPeriod);
		resp=getAllPerdiod();
		return resp;
	}

	

	@RequestMapping(value = { "/edit" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public PeriodResponse editPeriod(@RequestBody Map<String,String> periodDTO,HttpServletResponse response) {
		PeriodResponse resp=new PeriodResponse();
		Period period = periodManager.dtoToPeriod(periodDTO);
		period.setId(Long.parseLong(periodDTO.get("id")));
		if(periodManager.updatePeriod(period)){
			resp=getAllPerdiod();
			responseManager.prepareResponse(resp, PeriodResponse.OK);
		}
		else{
			responseManager.prepareResponse(resp, PeriodResponse.ERROR);
		}
		return resp;
	}
	

	@RequestMapping(value = { "/delete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public PeriodResponse deletePeriod(@RequestBody Map<String,String> periodDTO,HttpServletResponse response) {
		PeriodResponse resp=new PeriodResponse();
		
		try {
			resp=periodManager.deletePeriod(Long.parseLong(periodDTO.get("id")));
			if(resp.getResponseCode()==PeriodResponse.OK){
				resp=getAllPerdiod();
				responseManager.prepareResponse(resp, PeriodResponse.OK);
			}else if(resp.getResponseCode()!=PeriodResponse.PERIOD_HAVE_PLANNING){
				responseManager.prepareResponse(resp, PeriodResponse.ERROR);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (PeriodExistsException e) {
			e.printStackTrace();
		}

		return resp;
	}
	
	@RequestMapping(value = "/getNotSpecialPeriods/", method = RequestMethod.GET)
	public @ResponseBody PeriodResponse getNotSpecialPeriods() {
		return periodManager.getNotSpecialPeriods();
	}
	
	
	@RequestMapping(value = "/getAllLock", method = RequestMethod.GET)
	@ResponseBody
	public PeriodResponse getAllLocks() {
		return periodManager.getAllLocks();
		
	}

	@RequestMapping(value = { "/addLock" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public PeriodResponse addLock(@RequestBody Map<String,String> periodDTO,HttpServletResponse response) {
		PeriodResponse resp=new PeriodResponse();
		responseManager.prepareResponse(resp,PeriodResponse.OK);
		Period oPeriod = periodManager.dtoToPeriod(periodDTO);
		periodManager.addPeriod(oPeriod);
		resp=getAllLocks();
		return resp;
	}

	@RequestMapping(value = { "/editLock" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public PeriodResponse editLock(@RequestBody Map<String,String> periodDTO,HttpServletResponse response) {
		PeriodResponse resp=new PeriodResponse();
		Period period = periodManager.dtoToPeriod(periodDTO);
		period.setId(Long.parseLong(periodDTO.get("id")));
		if(periodManager.updatePeriod(period)){
			resp=getAllLocks();
			responseManager.prepareResponse(resp, PeriodResponse.OK);
		}
		else{
			responseManager.prepareResponse(resp, PeriodResponse.ERROR);
		}
		return resp;
	}
	

	@RequestMapping(value = { "/deleteLock" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public PeriodResponse deleteLock(@RequestBody Map<String,String> periodDTO,HttpServletResponse response) {
		PeriodResponse resp=new PeriodResponse();
		
		try {
			resp=periodManager.deletePeriod(Long.parseLong(periodDTO.get("id")));
			if(resp.getResponseCode()==PeriodResponse.OK){
				resp=getAllLocks();
				responseManager.prepareResponse(resp, PeriodResponse.OK);
			}else if(resp.getResponseCode()!=PeriodResponse.PERIOD_HAVE_PLANNING){
				responseManager.prepareResponse(resp, PeriodResponse.ERROR);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (PeriodExistsException e) {
			e.printStackTrace();
		}

		return resp;
	}
	
}
